<template>
	<div class="system-dict-data-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" draggable :close-on-click-modal="false" width="760px" style="padding-right:12px;">
			<el-form ref="dictTypeDialogFormRef" :model="state.dictDataForm" size="default" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="字典数据名" prop="name" :rules="[{required:true,message:'字典数据名不能为空',trigger:'blur'}]">
							<el-input v-model="state.dictDataForm.name" placeholder="请输入字典数据名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="字典数据值" prop="code" :rules="[{ required: true, message: '字典类型值不能为空', trigger: 'blur' }]">
							<el-input v-model="state.dictDataForm.code" placeholder="请输入字典数据值" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="state.dictDataForm.sortNum" :min="0" :max="999" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-radio-group v-model="state.dictDataForm.status">
								<el-radio :label="0">启用</el-radio>
								<el-radio :label="1">禁用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="描述">
							<el-input v-model="state.dictDataForm.remarks" clearable type="textarea" placeholder="请输入描述" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDictDataDialog">
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { useDictApi } from '/@/api/dict';

// 定义子组件向父组件传值/事件
//const emit = defineEmits(['refresh']);
const emits = defineEmits(['getTableData']);
// const props = defineProps({
// 	title:String,
// 	dictType: DictTypeForm,
// });
// 定义变量内容
const dictTypeDialogFormRef = ref();
const state = reactive({
	dictDataForm: {} as DictDataForm,
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: DictDataForm) => {
	if (type === 'edit') {
		//console.log('rowe:'+JSON.stringify(row));
		state.dictDataForm = row;
		state.dialog.title = '修改字典数据';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dictDataForm = row;
		state.dialog.title = '新增字典数据';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	posDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	// getMenuData();
};
// 关闭弹窗
const closeDialog = () => {
	emits('getTableData', true);
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	// closeDialog();
	// emit('refresh');
	dictTypeDialogFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		// if(props.dictTypeId>0){
			//state.dictDataForm.dictTypeId=props.dictTypeId;
			if (state.dictDataForm.id != undefined && state.dictDataForm.id > 0) {
				useDictApi().editDictData(state.dictDataForm).then(async (res: AjaxResult) => {
					if (res.success == true) {
						ElMessage.success('更新成功');
						emits('getTableData');
						closeDialog();
					}
					else {
						ElMessage.warning('更新失败：' + res.message);
					}
				});
			} else {
				useDictApi().addDictData(state.dictDataForm).then(async (res: AjaxResult) => {
					if (res.success == true) {
						ElMessage.success('新增成功');
						emits('getTableData');
						closeDialog();
					}
					else {
						ElMessage.warning('新增失败：' + res.message);
					}
				});
			}
		// }
		// else{
		// 	ElMessage.warning('请先选择字典类型');
		// }
		//closeDialog();
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-pos-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
